package com.education.platform.management.controller.question;

import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.QuestionBankDO;
import com.education.platform.management.service.QuestionBankService;
import com.education.platform.management.vo.request.QuestionBankQueryVO;
import com.education.platform.management.vo.request.QuestionBankUpdateVO;
import com.education.platform.management.vo.response.QuestionBankPageInfoVO;
import com.education.platform.management.vo.response.QuestionBankResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* describe: 题库
* @author : tomato
* @date: 2020-06-29 10:28:07.918
**/
@RestController
@Api("题库")
@RequestMapping("questionBank")
@Validated
public class QuestionBankController {

    private static final Logger LOGGER = LoggerFactory.getLogger(QuestionBankController.class);

	@Autowired
    private QuestionBankService questionBankService;
    /**
     * 分页查询
     * @param questionBankQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public QuestionBankPageInfoVO selectPageList(@RequestBody @Valid QuestionBankQueryVO questionBankQueryVO) {
        QuestionBankDO questionBank = BeanUtil.convert(questionBankQueryVO, QuestionBankDO.class);
        Page<QuestionBankResponseVO> page = PageHelper.startPage(questionBankQueryVO.getPageNum(), questionBankQueryVO.getPageSize());
        List<QuestionBankDO> questionBankList = questionBankService.getList(questionBank);


        QuestionBankPageInfoVO questionBankPageInfo = new QuestionBankPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), questionBankPageInfo);
        questionBankPageInfo.setPageNum(questionBankQueryVO.getPageNum());
        List<QuestionBankResponseVO> voList = new ArrayList<>();

        questionBankList.forEach(m->{
            QuestionBankResponseVO responseVO =  BeanUtil.convert(m, QuestionBankResponseVO.class);

            if (m.getType() == 2) {
                //简单题
                //答案
                responseVO.setShortAnswer(m.getChoiceAnswer());
            } else {
                //选择题
                //答案
                List<Integer> choice=Arrays.asList(m.getChoiceAnswer().split("==")).
                        stream().map(Integer::valueOf).collect(Collectors.toList());
                responseVO.setAnswerIndexList(choice);
                //选项
                responseVO.setOptions(Arrays.asList(m.getOptionA().split("==")));
            }

            if(StringUtils.isNotEmpty(m.getMode())){
                String[]modeStr = m.getMode().split(",");
                List<Integer> modes = Arrays.asList(modeStr).parallelStream().map(x->Integer.valueOf(x)).collect(Collectors.toList());
                responseVO.setModes(  modes );
            }

            voList.add(responseVO);
        });
        questionBankPageInfo.setList(voList);
        return questionBankPageInfo;
    }


    @ApiOperation(value = "新增/修改题目")
    @PostMapping("save")
    public Boolean save(@RequestBody @Valid QuestionBankUpdateVO vo) {
       return questionBankService.save( vo);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public void delete(@RequestBody @Valid List<Integer> ids) {
        questionBankService.deleteByIds(ids);
    }
}
